<?php
//首页显示
namespace Home\Controller;
use Think\Controller;
class ApiController extends CommonController {

  public function auto_jiesuan(){
  	//自动结算一小时扫描数据库一次,超过7天时间自动结算
	file_put_contents("dingshi.txt", date("Y-m-d H:i:s") . "执行定时任务！自动结算" . "\r\n", FILE_APPEND);
	$rs=M('order')->field('order_id,store_id,buyer_id,finnshed_time,order_amount,daiyan_sale,buyer_id,coupon')->where("order_state='40' and refund_state=0 and lock_state=0" )->select();

	foreach($rs as $key=>$value){
		$endtime=$value['finnshed_time']+604800;
		$now=time();
		if($now>$endtime){
			//开启  锁定
			$sssdata['lock_state']=1;
    		M('order')->where("order_id=".$value['order_id'])->save($sssdata);
    		M('order_goods')->where("order_id=".$value['order_id'])->save($sssdata);
			//店铺收入打款入账
    		$youhui_price=get_coupon_price($value['buyer_id'],$value['coupon']);
    		$count_price=$value['order_amount']-$value['daiyan_sale']-$youhui_price;
    		$store_money_log=M('store_money_log')->where("order_id=".$value['order_id'])->select();
    		if($store_money_log==null){
    			if($count_price>0){
	    			$money_rs=M('store')->where("id=".$value['store_id'])->setInc("money",$count_price);
	    			if($money_rs){
		    			$store_log['store_id']=$value['store_id'];
		    			$store_log['addtime']=time();
		    			$store_log['money']=$count_price;
		    			$store_log['type']=1;
		    			$store_log['other']='订单结算收入';
		    			$store_log['order_id']=$value['order_id'];
		    			$store_log['money_type']=1;
		    			M('store_money_log')->add($store_log);
		    		}
	    		}else{

	    			$store_log['store_id']=$value['store_id'];
	    			$store_log['addtime']=time();
	    			$store_log['money']=0;
	    			$store_log['type']=1;
	    			$store_log['other']='该订单免费';
	    			$store_log['order_id']=$value['order_id'];
	    			$store_log['money_type']=1;
	    			M('store_money_log')->add($store_log);

	    		}
    		}
    		//代言打款
			$order_goods_list=M('order_goods')->where("order_id=".$value['order_id']." and recommend_type=0")->select();

			if($order_goods_list){

				foreach($order_goods_list as $k=>$v){
					$user_money_log=M('users_log')->where("order_id=".$value['order_id']." and shop_id=".$v['goods_id'])->select();

					if($v['recommend_id']!=null&&$v['recommend_price']>0&&$user_money_log==null){
						$user_money_rs=M('users')->where("id=".$v['recommend_id'])->setInc("money",$v['recommend_price']);
						if($user_money_rs){
							$user_log['uid']=$v['recommend_id'];
							$user_log['addtime']=time();
							$user_log['money']=$v['recommend_price'];
							$user_log['type']=1;
							$user_log['other']='红包收入';
							$user_log['store_id']=$value['store_id'];
							$user_log['order_id']=$value['order_id'];
							$user_log['shop_id']=$v['goods_id'];
							$user_log['money_type']=2;
							M('users_log')->add($user_log);
						}
					}
				}
			}

		}
	}
  }

  public function auto_sendinfo(){
  	//自动短信自动提醒
	file_put_contents("dingshi.txt", date("Y-m-d H:i:s") . "执行定时任务！短信自动提醒" . "\r\n", FILE_APPEND);
	$rs=M('order')->field('order_id,order_sn,store_id,buyer_id,finnshed_time,order_amount,daiyan_sale,buyer_id,coupon')->where("order_state='30' and refund_state=0 and auto_sendinfo=0" )->select();
	foreach($rs as $key=>$value){
		$endtime=$value['shipping_time']+172800;
		$now=time();
		if($now>$endtime){
			$userinfo=M('users')->find($value['buyer_id']);
    		$data['username']=C('duanxin.username');
			$data['password']=C('duanxin.password');
			$data['mobile']=$userinfo['phone'];
			$data['content']='【58代言】 您的订单：'.$value['order_sn'].'已经发货，请即时确认收货.';
			$url=C('duanxin.url');
			$rs=post_url($url,$data);

			$xml = simplexml_load_string($rs);
			$rs = json_decode(json_encode($xml),TRUE);
			if($rs['resultcode']==0){
				$data['auto_sendinfo']=1;
    			M('order')->where('order_id='.$value['order_id'])->save($data);
			}
    	}
	}
  }

  public function auto_shouhuo(){
  	//自动收货执行定时任务！自动收货
	file_put_contents("dingshi.txt", date("Y-m-d H:i:s") . "执行定时任务！自动收货" . "\r\n", FILE_APPEND);
	$rs=M('order')->field('order_sn,buyer_id,order_id,shipping_time,delay_time,add_time')->where("order_state='30' and refund_state=0" )->select();
	foreach($rs as $key=>$value){
		$endtime=$value['shipping_time']+604800+$value['delay_time'];
		$now=time();
		if($now>$endtime){
			$close_data['order_state']='40';
    		$close_data['finnshed_time']=time();
    		M('order')->where('order_id='.$value['order_id'])->save($close_data);
    		//收货代言通知
				$uid=$value['buyer_id'];
                $order_add_time=$value['add_time'];
		        $user_info=M('users')->find($uid);

				if($user_info['wx_h5openid']){
					$openid=$user_info['wx_h5openid'];
					$url='https://m.58daiyan.com/user/daiyan.html';
					$keyword1=date('Y年m月d日  H:i',$order_add_time);
					$keyword2=$value['order_sn'];
					send_info_daiyan($openid,$url,$keyword1,$keyword2);
				}
		}
	}
  }
   public function back_order(){
   		//拼团订单 不成功  24小时退款
	file_put_contents("dingshi.txt", date("Y-m-d H:i:s") . "执行定时任务！拼团24小时退款" . "\r\n", FILE_APPEND);
	$redis =connectRedis();
	$redis->set('back_order_list',null);
	$order_list = $redis->get('back_order_list');
	M('order')->where('refund_state=4 and refund_type=1')->setField('order_state','20');
	if($order_list==null){
		$rs=M('order')->field('order_id,order_sn,add_time,tuanselect,tuannum')->where("order_state='20' and tuanselect=1 and refund_state=0 and tuan_is_first_id='0'")->select();
		$newdata=array();
		if($rs){
			foreach($rs as $key=>$value){
				$num=get_tuan($value['order_id']);
				if($num<$value['tuannum']){
					$data['order_id']=$value['order_id'];
					$data['order']=$value['order_sn'];
					$data['add_time']=$value['add_time'];
					$data['neednum']=$value['tuannum'];
					$data['num']=$num;
					$data['clostime']=60*60*24;
					$newdata[]=$data;
				}
			}
		}

		//待处理订单
		if($newdata){


			$newdata=json_encode($newdata);
			$redis->set('back_order_list',$newdata);
			$order_list = $redis->get('back_order_list');
		}
	}

	if($order_list){
		$order_list=json_decode($order_list,true);
		foreach($order_list as $key=>$v){
			$time=time();
			$guoqi_time= (int)$v['add_time']+$v['clostime'];

			if($time>$guoqi_time){

				$o_info=M('order')->find($v['order_id']);
				if($o_info['order_state']=='20'&&$o_info['refund_state']==0){
					$o_data['order_id']=$v['order_id'];
					$o_data['refund_state']=4;//同意退款
					$o_data['refund_amount']=$o_info['order_amount'];//申请退款
					$o_data['refund_reason']='拼团不成功';//申请退款
					$o_data['refund_type']=1;//申请退款
					$o_data['refund_time']=time();//申请退款
					$o_data['refund_agreen_time']=time();//申请退款
					M('order')->save($o_data);


					$son_data['refund_state']=4;//同意退款
					$son_data['refund_amount']=$o_info['order_amount'];//申请退款
					$son_data['refund_reason']='拼团不成功';//申请退款
					$son_data['refund_type']=1;//申请退款
					$son_data['refund_time']=time();//申请退款
					$son_data['refund_agreen_time']=time();//申请退款
					$where['order_state']='20';
					$where['tuan_is_first_id']=$v['order_id'];
					M('order')->where($where)->save($son_data);
					unset($order_list[$key]);
				}

			}
		}

	}
   }

   public function close_order(){
   		//待处理订单详情表 定时关闭 普通订单24小时  团购订单3分钟
	file_put_contents("dingshi.txt", date("Y-m-d H:i:s") . "执行定时任务！订单定时关闭" . "\r\n", FILE_APPEND);
	$redis =connectRedis();
	$redis->set('close_order_list',null);
	$close_order_list = $redis->get('close_order_list');
	if($close_order_list==null){
		$close_rs=M('order')->field('order_id,order_sn,add_time,tuanselect')->where("order_state='10'")->select();

		$close_newdata=array();
		if($close_rs){
			foreach($close_rs as $key=>$value){
				if($value['tuanselect']==1){
					$close_data['order_id']=$value['order_id'];
					$close_data['order']=$value['order_sn'];
					$close_data['add_time']=$value['add_time'];
					$close_data['clostime']=60*3;
					$close_newdata[]=$data;
				}else{
					$close_data['order_id']=$value['order_id'];
					$close_data['order']=$value['order_sn'];
					$close_data['add_time']=$value['add_time'];
					$close_data['clostime']=60*60*24;
					$close_newdata[]=$data;
				}
			}
		}

		//待处理订单
		if($close_newdata){
			$close_newdata=json_encode($close_newdata);
			$redis->set('close_order_list',$close_newdata);
			$close_order_list = $redis->get('close_order_list');
		}
	}

	if($close_order_list){
		$close_order_list=json_decode($close_order_list,true);
		foreach($close_order_list as $key=>$v){
			$time=time();
			$guoqi_time= (int)$v['add_time']+$v['clostime'];
			if($time>$guoqi_time){
				$o_info=M('order')->field('order_state')->find($v['order_id']);
				if($o_info['order_state']=='10'){
					$o_data['order_id']=$v['order_id'];
					$o_data['order_state']='0';
					M('order')->save($o_data);
					$action_data['order_id']=$v['order_id'];
					$action_data['action']='订单超时';
					$action_data['uid']=0;
					$action_data['user_name']='系统';
					$action_data['order_status']='0';
					$action_data['order_status_text']='订单超时关闭';
					$action_data['action_time']=time();
					M('order_action')->add($action_data);
				}
				unset($close_order_list[$key]);
			}
		}
		$close_order_list=json_encode($close_order_list);
		$redis->set('close_order_list',$close_order_list);

	}

   }
}
?>
